<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use App\Model\Goods;
use App\Model\Goodstype;
use App\Model\Goodsspecs;
use App\Http\Controllers\Api\GoodsDetailSelect;
use App\Http\Controllers\Api\GoodsDetailAjax;
use App\Http\Controllers\Api\IndexApi;
use App\Http\Controllers\Home\ShopCartController;
use App\Http\Controllers\Home\ShowLinksController;


/**
 * 详情页
 * 黎子聪
 */
class GoodsDetailController extends Controller
{

    /**
     * 显示商品详情页
     * data id
     */
    public function ShowGoodsDetailPage($id)
    {
        $goodsstatus = Goods::select('goods_status')->where('id', $id)->first();

        if ($goodsstatus->goods_status !== 0) {
              echo "<script>alert('该商品已下架')</script>";
        }

        // 得到商品类
        $goods = new GoodsDetailSelect;
        $pageajax = new GoodsDetailAjax;
        $type = new IndexApi;
        $index = new ShowLinksController;

        // 获取站点信息
        $sate = IndexApi::getSate();
        $linksArray = $index->index();

        // 获取促销商品
        $promategoods = $goods->GetPromateData($id);

        // 获取商品详情
        $goodsdetail = $goods->GetColorDetail($id);

        // 取出商品内的介绍图片
        $goodspic = $goods->GoodsPic($id);

        //获取所有分类信息
        $types = $type->getAllTypes();

        //显示购物车商品数量
        $shopCartController = new ShopCartController;

        //购物车数量变动
        $originalNum = $shopCartController->showCartNumber();

        // 获取商品介绍
        $content = $goods->GoodsContent($id);


        // 判断是否为促销
        if ($promategoods) {
            // 将详情商品数组分开
            $color = $goodsdetail[0];
            $size = $goodsdetail[1];
            return view('Home.goods-detail', ['content' => $content, 'originalNum' => $originalNum, 'goods' => $promategoods, 'color' => $color, 'size' => $size, 'pic' => $goodspic, 'types' => $types, 'sate' => $sate, 'linksArray' => $linksArray, 'type' => 'promate']);
        }

        // 获取热销商品
        $hotgoods = $goods->GetHotData($id);

        if ($hotgoods) {
            // 将详情商品数组分开
            $color = $goodsdetail[0];
            $size = $goodsdetail[1];
            return view('Home.goods-detail', ['content' => $content, 'originalNum' => $originalNum, 'goods' => $hotgoods, 'color' => $color, 'size' => $size, 'pic' => $goodspic, 'types' => $types, 'sate' => $sate, 'linksArray' => $linksArray, 'type' => 'hotgoods']);
        }

        // 获取普通商品
        $generalgoods = $goods->GetGenearlData($id);

        if ($generalgoods) {
            // 将详情商品数组分开
            $color = $goodsdetail[0];
            $size = $goodsdetail[1];
            return view('Home.goods-detail', ['content' => $content, 'originalNum' => $originalNum, 'goods' => $generalgoods, 'color' => $color, 'size' => $size, 'pic' => $goodspic, 'types' => $types, 'sate' => $sate, 'linksArray' => $linksArray, 'type' => 'generalgoods']);
        }
        // 你他娘的输入的啥玩意
        return view('notfound');

    }

    /**
     * 处理查询该颜色尺寸的处理方法
     * data $id $color
     */
    public function ShowSize(Request $request)
    {
        $size = new GoodsDetailAjax;

        $result = $size->GetColorunderTheSize($request->id, $request->color);

        return $result;
    }

    // 返回所需要的差集
    public function PoorSet(Request $request)
    {
        // 将字符串切割成数组
        $colorsize = explode(',', $request->colorsize );
        $sizes = explode(',', $request->sizes);

        // 比较差集
        $size = array_diff($sizes, $colorsize);

        // 重新排序索引
        $result = array_values($size);
        // 返回数据
        return $result;
    }

    // 返回该商品的库存
    public function GetGoodsDetailReserve(Request $request)
    {
        // 获取实例
        $goodsdetail = new GoodsDetailAjax;

        // 得到库存
        $result = $goodsdetail->GetGoodsReserve($request->goods_id, $request->sizeinfo, $request->color);

        // 返回库存
        return $result;
    }

    // 得到详情商品id
    public function GetGoodsDetailInfo(Request $request)
    {
        // 获取实例
        $goodsdetail = new GoodsDetailAjax;

        // 得到id
        $result = $goodsdetail->GetGoodsInfo($request->goods_id, $request->sizeinfo, $request->color);

        // 返回id
        return $result;

    }

    // 商品介绍图片
    public function GoodsDetailPic(Request $request)
    {
        // 得到ajax类
        $pageajax = new GoodsDetailSelect;

        // 得到介绍
        $goodsdetailinfo = $pageajax->GoodsContent($request->id);

        // 解码
        $goodsdetailinfo = JSON_decode($goodsdetailinfo['goods_content']);

        // 返回数据
        return $goodsdetailinfo;
    }
}
